package pl.ptemich.tankwars.states;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import pl.ptemich.tankwars.GraphicConstants;
import pl.ptemich.tankwars.objects.Ball;
import pl.ptemich.tankwars.objects.CanonBall;
import pl.ptemich.tankwars.objects.Paddle;
import pl.ptemich.tankwars.tools.StateTools;

public class PinballState extends RegisteredState implements MouseListener {
  
  private static final int BALLS_COUNT = 1;
  
  private static final int MOUSE_DEAD_ZONE = 1;
 
  
  private Paddle paddle;
  
  private List<Ball> balls;
  
  private CanonBall canonBall;

  
  @Override
  public void mouseMoved(int oldx, int oldy, int newx, int newy) {    
    super.mouseMoved(oldx, oldy, newx, newy);
    int dist = oldx - newx;
    if (Math.abs(dist) > MOUSE_DEAD_ZONE) {
      if (dist > 0) {
        paddle.moveLef();
      } else {
        paddle.moveRight();
      }
    }
  }
  
  @Override
  public void init(GameContainer container, StateBasedGame game) throws SlickException {
    GraphicConstants.initResources();
    paddle = new Paddle(GraphicConstants.SCENE_WIDTH, GraphicConstants.SCENE_HEIGHT);
    balls = new ArrayList<Ball>();
    for (int ballIndex = 0; ballIndex < BALLS_COUNT; ballIndex++) {
      balls.add(new Ball(GraphicConstants.SCENE_WIDTH, GraphicConstants.SCENE_HEIGHT));
    }
    
    canonBall = new CanonBall(GraphicConstants.SCENE_WIDTH, GraphicConstants.SCENE_HEIGHT);
  }

  @Override
  public void render(GameContainer container, StateBasedGame game, Graphics graphics)
      throws SlickException {
    //graphics.clear();
    for (Ball ball : balls) {
      ball.render(graphics);   
    }
    paddle.render(graphics);
    canonBall.render(graphics);
  }

  @Override
  public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {    

//    int fixedDelta = delta;
//    if (fixedDelta > 16) {
//      fixedDelta = 16;
//    }

    for (Ball ball : balls) {
      ball.update(delta);
    }
    
    Input input = container.getInput();
    if (input.isKeyDown(Input.KEY_ESCAPE)) {
      StateTools.stateTransition(MenuState.class);
    }

    paddle.update(delta);
  }

//  @Override
//  public void keyPressed(int key, char c) {    
//    super.keyPressed(key, c);
//    
//    switch (key) {
//      case Input.KEY_UP:
//        break;
//      case Input.KEY_DOWN:
//        break;
//    }
//  } 
  
//  @Override
//  public void mouseClicked(int button, int x, int y, int clickCount) {
//    // TODO Auto-generated method stub
//    super.mouseClicked(button, x, y, clickCount);
//  }
//  
//  @Override
//  public void mouseReleased(int button, int x, int y) {
//    // TODO Auto-generated method stub
//    super.mouseReleased(button, x, y);
//  }
}
